package smartclass.dao;

import java.util.ArrayList;

import smartclass.data.ManagerDataBaseHelper;
import smartclass.data.TableItem;
import smartclass.entity.StudentInfo;
import smartclass.entity.TeacherInfo;
import smartclass.present.LogInActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class TeacherDataDao {

	static TeacherDataDao teacherDao;
	ManagerDataBaseHelper db;
	String tableName;

	public TeacherDataDao() {
		// TODO Auto-generated constructor stub
		db = LogInActivity.db;
		tableName = ManagerDataBaseHelper.TEACHER_TABLE;
	}

	public static TeacherDataDao getInstance() {
		if (teacherDao == null) {
			teacherDao = new TeacherDataDao();
		}
		return teacherDao;
	}

	// Add Item
	public boolean addTeacher(TeacherInfo teacher) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");

		ContentValues value = new ContentValues();
		try {
			value.put(TableItem.ID, teacher.getId());
			System.out.println(teacher.getId());
			value.put(TableItem.NAME, teacher.getName());
			value.put(TableItem.USER_NAME, teacher.getUserName());
			value.put(TableItem.POSITION, teacher.getPosition());
			value.put(TableItem.SUBJECT, teacher.getSubject());
			value.put(TableItem.ADDRESS, teacher.getAddress());
			value.put(TableItem.GENDER, teacher.getGender());
			value.put(TableItem.PASS_WORD, teacher.getPassword());
			value.put(TableItem.PHONE_NUMBER, teacher.getPhoneNumber());
			data.insert(tableName, TableItem.ID, value);
			data.close();
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
	}

	// Get Item
	public TeacherInfo getTeacher(int id) {
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		TeacherInfo teacherInfo = new TeacherInfo();
		Cursor c = null;

		try {
			String[] params = new String[] { String.valueOf(id) };
			String sql = "SELECT * FROM " + tableName + " WHERE "
					+ TableItem.ID + "=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			teacherInfo.setId(Integer.parseInt(c.getString(0)));
			teacherInfo.setName(c.getString(1));
			teacherInfo.setUserName(c.getString(2));
			teacherInfo.setPosition(c.getString(3));
			teacherInfo.setSubject(c.getString(4));
			teacherInfo.setAddress(c.getString(5));
			teacherInfo.setGender(c.getString(6));
			teacherInfo.setPassword(c.getString(7));
			teacherInfo.setPhoneNumber(c.getString(8));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return teacherInfo;
	}

	// Get All Item
	public ArrayList<TeacherInfo> getAllTeacher() {
		ArrayList<TeacherInfo> allTeacher = new ArrayList<TeacherInfo>();
		Cursor c = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		String sql = "SELECT * FROM " + tableName;
		c = db.getReadableDatabase().rawQuery(sql, null);
		TeacherInfo teacherInfo = null;
		if (c.moveToFirst()) {
			do {
				teacherInfo = new TeacherInfo();
				teacherInfo.setId(c.getInt(0));
				teacherInfo.setName(c.getString(1));
				System.out.println("Name Teacher: " + c.getString(1));
				teacherInfo.setUserName(c.getString(2));
				teacherInfo.setPosition(c.getString(3));
				teacherInfo.setSubject(c.getString(4));
				teacherInfo.setAddress(c.getString(5));
				teacherInfo.setGender(c.getString(6));
				teacherInfo.setPassword(c.getString(7));
				teacherInfo.setPhoneNumber(c.getString(8));
				allTeacher.add(teacherInfo);
			} while (c.moveToNext());
		}
		return allTeacher;
	}

	// Get password by id
	public String getPassword(int id) {
		String password = null;
		SQLiteDatabase data = db.getReadableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		Cursor c = null;
		try {
			String[] params = new String[]{ String.valueOf(id)};
			String sql = "SELECT * FROM "+tableName+" WHERE "+TableItem.ID+"=?";
			c = data.rawQuery(sql, params);
			c.moveToFirst();
			password = c.getString(7);
		} catch (Exception e) {
			
		}
		return password;
	}

	// Edit Item

	public boolean updateTeacher(TeacherInfo teacher) {
		SQLiteDatabase data = db.getWritableDatabase();
		data.execSQL("PRAGMA foreign_keys = ON;");
		ContentValues value = new ContentValues();
		value.put(TableItem.ID, teacher.getId());
		value.put(TableItem.NAME, teacher.getName());
		value.put(TableItem.USER_NAME, teacher.getUserName());
		value.put(TableItem.POSITION, teacher.getPosition());
		value.put(TableItem.SUBJECT, teacher.getSubject());
		value.put(TableItem.ADDRESS, teacher.getAddress());
		value.put(TableItem.GENDER, teacher.getGender());
		value.put(TableItem.PASS_WORD, teacher.getPassword());
		value.put(TableItem.PHONE_NUMBER, teacher.getPhoneNumber());

		data.update(tableName, value, TableItem.ID + "=?",
				new String[] { String.valueOf(teacher.getId()) });
		data.close();
		return true;
	}

	// Delete Item

	public boolean deleteDirection(int id) {
		String sql = "DELETE FROM " + tableName + " WHERE " + TableItem.ID
				+ "=" + id;
		db.getWritableDatabase().execSQL(sql);
		return true;
	}

}
